package com.test.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.test.base.BaseAction;
import com.test.domain.Department;
import com.test.util.DepartmentUtils;
/**
 * 所需的service已经从BaseAction中继承过来了
 * @author Administrator
 *
 */
public class DepartmentAction extends BaseAction<Department> {
	private static final long serialVersionUID = 1L;
	//由于模型中没有parentId,这里创建一个parentId;
	private Integer parentId;
	public Integer getParentId() {
		return parentId;
	}
	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}
	/*
	 * 1，列表页面只显示一层的（同级的）部门数据，默认显示最顶级的部门列表。
	   2，点击部门名称，可以查看此部门相应的下级部门列表。
	 */
	public String list()throws Exception{
		List<Department> departmentList = null;
		//如果没有上级部门
		if(parentId==null){
			//默认显示顶级部门列表
			departmentList = departmentService.findTopList();
		}else{
			//点击部门后，显示所有下级部门列表
			departmentList = departmentService.findChildren(parentId);
			Department parent = departmentService.findDepartmentById(parentId);
			ActionContext.getContext().put("parent", parent);
		}
		ActionContext.getContext().put("departmentList", departmentList);
		return "list";
	}
	public String delete()throws Exception{
		departmentService.deleteDepartment(model.getId());
		return "toList";
	}
	public String addUI()throws Exception{
		//准备部门列表数据
//		List<Department> departmentList = departmentService.findAll();
//		ActionContext.getContext().put("departmentList", departmentList);
		//使用树状结构显示部门列表
		List<Department> topList = departmentService.findTopList();
		//添加的时候，所有部门都有可能是我的上级部门，所以没有要移除的部门
		List<Department> departmentList = DepartmentUtils.getAllDepartments(topList,null);
		ActionContext.getContext().put("departmentList", departmentList);
		return "saveUI";
	}
	public String add()throws Exception{
		Department parent = departmentService.findDepartmentById(parentId);
		model.setParent(parent);
		departmentService.addDepartment(model);
		return "toList";
	}
	public String editUI()throws Exception{
		//准备当前选中部门的相关数据
		Department department = departmentService.findDepartmentById(model.getId());
		ActionContext.getContext().getValueStack().push(department);
		//如果有上级部门
		if(department.getParent()!=null){
			this.parentId = department.getParent().getId();
		}
		//准备部门列表
//		List<Department> departmentList = departmentService.findAll();
//		ActionContext.getContext().getValueStack().push(departmentList);
		//使用树状结构来显示部门列表
		List<Department> topList = departmentService.findTopList();
		//注意，修改时，不能在上级部门列表中，不能出现自己及自己的子孙部门。因为自己和自己的子部门不可能又还是自己的上级
		//但是添加的时候，所有部门都有可能是我的上级
		//所以在获取部门列表的时候，需要移除本部门，这样也就不会遍历本部门的子部门。
		List<Department> departmentList = DepartmentUtils.getAllDepartments(topList,department);
		ActionContext.getContext().put("departmentList",departmentList);
		return "saveUI";
	}
	public String edit() throws Exception{
		Department department = departmentService.findDepartmentById(model.getId());
		department.setName(model.getName());
		department.setDescription(model.getDescription());
		
		//处理上级部门
		Department parent = departmentService.findDepartmentById(parentId);
		department.setParent(parent);
		departmentService.updateDepartment(department);
		return "toList";
	}
}
